home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 696 / clitools / time / time.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  2KB  |  105 lines

  1. ;/* time - compiles with SAS/C 5.10a by executing this file
  2. lc -cfisq -v -b0 -j73 -O -M time
  3. blink time.o to time lib lib:lcnb.lib lib:amiga.lib SC ND VERBOSE
  4. quit ;*/
  5. /*
  6. *    time - measure time command takes to complete.
  7. *
  8. *    Usage: time <command-and-its-args> 
  9. *
  10. *    Martin W. Scott, 4/92.
  11. */
  12. #include <exec/types.h>
  13. #include <dos/dos.h>
  14. #include <dos/rdargs.h>
  15.  
  16. #include <proto/exec.h>        /* use PRAGMAS */
  17. #include <proto/dos.h>
  18.  
  19. void main(void);
  20. void PrintDateStamp(struct DateStamp *);
  21. void SubDateStamp(struct DateStamp *, struct DateStamp *);
  22.  
  23.  
  24. char version_str[] = "$VER: time v1.0";
  25. struct DosLibrary *DOSBase;
  26. struct RDArgs rdargs;
  27.  
  28. void main(void)        /* entry: start command and print how long it took */
  29. {
  30.     struct DateStamp before, after;
  31.     struct RDArgs *readargs;
  32.     LONG rargs[1];
  33.  
  34.     if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L))
  35.     {
  36.         rdargs.RDA_ExtHelp = "Usage: time <command>\n prints time command takes to execute, in form HH:MM:SS.SS\n";
  37.         if (readargs = ReadArgs("COMMAND/A/F", rargs, &rdargs))
  38.         {
  39.             DateStamp(&before);
  40.             SystemTagList((char *)rargs[0], NULL);
  41.             DateStamp(&after);
  42.             SubDateStamp(&after,&before);
  43.             PrintDateStamp(&after);
  44.         }
  45.         else PrintFault(IoErr(), "time");
  46.  
  47.         CloseLibrary(DOSBase);
  48.     }
  49.  
  50. } /* main */
  51.  
  52. #include <stdarg.h>
  53. LONG __stdargs DosFPrintf(BPTR, char *, ...);
  54.  
  55. LONG __stdargs DosFPrintf(BPTR fh, char *s, ...)    /* stub for VFPrintf */
  56. {
  57.     va_list ap;
  58.     va_start(ap,s);
  59.     return VFPrintf(fh, s, (long *)ap);
  60. }
  61.  
  62.  
  63. #define TICKS_PER_MINUTE    (TICKS_PER_SECOND*60)
  64. #define MINUTES_PER_DAY        (60*24)
  65.  
  66.  
  67. void PrintDateStamp(struct DateStamp *ds)    /* print datestamp as HH:MM:SS.SS */
  68. {
  69.     BPTR con;        /* gets set to stderr */
  70.     LONG h,m,s,hs;
  71.  
  72.     h = ds->ds_Days*24 + ds->ds_Minute / 60;        /* hours */
  73.     m = ds->ds_Minute % 60;            /* minutes */
  74.     s = ds->ds_Tick / TICKS_PER_SECOND;        /* seconds */
  75.                             
  76.     hs = (100 *                    /* hundreths of a second */ 
  77.         (ds->ds_Tick % TICKS_PER_SECOND)) / TICKS_PER_SECOND;
  78.  
  79.     if (con = Open("*", MODE_OLDFILE))
  80.     {
  81.         DosFPrintf(con, "time %ld:%02ld:%02ld.%02ld\n", h,m,s,hs);
  82.         Close(con);
  83.     }
  84. }
  85.  
  86. void SubDateStamp(struct DateStamp *ds, struct DateStamp *amount)
  87. {
  88.     /* subtract amount from ds */
  89.  
  90.     if (ds->ds_Tick < amount->ds_Tick)
  91.     {
  92.         ds->ds_Tick += TICKS_PER_MINUTE;
  93.         ds->ds_Minute--;
  94.     }
  95.  
  96.     if (ds->ds_Minute < amount->ds_Minute)
  97.     {
  98.         ds->ds_Minute += MINUTES_PER_DAY;
  99.         ds->ds_Days--;
  100.     }
  101.  
  102.     ds->ds_Days -= amount->ds_Days;
  103.     ds->ds_Minute -= amount->ds_Minute;
  104.     ds->ds_Tick -= amount->ds_Tick;
  105. }